package de.lulusoft.anothertorrent.gui;

import java.net.URL;

import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;

/**
 * The activator class controls the plug-in life cycle
 */
public class AnotherTorrentGui extends AbstractUIPlugin {

	// The plug-in ID
	public static final String PLUGIN_ID = "de.lulusoft.anothertorrent.gui";

	// The shared instance
	private static AnotherTorrentGui plugin;
	
	/** The relative path to the images directory. */
	private static final String IMAGES_PATH = "icons/";
	
	/**
	 * The constructor
	 */
	public AnotherTorrentGui() {
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
	 */
	public void start(BundleContext context) throws Exception {
		super.start(context);
		plugin = this;
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
	 */
	public void stop(BundleContext context) throws Exception {
		plugin = null;
		super.stop(context);
	}

	/**
	 * Returns the shared instance
	 *
	 * @return the shared instance
	 */
	public static AnotherTorrentGui getDefault() {
		return plugin;
	}
	
	 /**
     * Creates an image by loading it from a file in
     * the plugin's resources directory.
     *
     * @param imagePath The path to the image file to load.
     *     The image path must not include the plugin's images
     *     path location defined in <code>IMAGES_PATH</code>.
     *     If you want to load <code>IMAGES_PATH/myimage.gif</code>,
     *     simply pass <code>myimage.gif</code> to this method.
     *
     * @return The image object loaded from the image file
     */
    public static Image createImage(String imagePath)
    {
         final Bundle pluginBundle =
              Platform.getBundle(PLUGIN_ID);


         final Path imageFilePath =
              new Path(IMAGES_PATH + imagePath);


         URL imageFileUrl =
        	 FileLocator.find(pluginBundle, imageFilePath, null);

         if (imageFileUrl == null)
         {
        	 imageFileUrl = pluginBundle.getResource(IMAGES_PATH + imagePath);
         }
         
         return
              ImageDescriptor
                   .createFromURL(imageFileUrl)
                   .createImage();
    }

}
